package com.twentytwok.hospitalsystem22K.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.twentytwok.hospitalsystem22K.common.R;
import com.twentytwok.hospitalsystem22K.mapper.DiscussionMapper;
import com.twentytwok.hospitalsystem22K.pojo.Discussion;
import com.twentytwok.hospitalsystem22K.service.DiscussionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("discussionService")
public class DiscussionServiceImpl implements DiscussionService {


    @Autowired
    private DiscussionMapper discussionMapper;

    /**
     * 分页操作
     *
     * @param map Map 类型
     * @return 实体 R 类型
     */
    @Override
    public R pageDiscussion(Map<String, Object> map) {
        map.put("delete", 3);
        PageHelper.startPage(Integer.parseInt(map.get("page").toString()),Integer.parseInt(map.get("limit").toString()));
        List<Discussion> discussions = discussionMapper.selectAll();

        PageInfo<Discussion> pageInfo = new PageInfo<>(discussions);

        R r = new R();

        r.setCode(0);
        r.setMsg("");
        r.setCount((int) pageInfo.getTotal());
        r.setData(pageInfo.getList());

        return r;
    }

    /**
     * 新增一个评论
     *
     * @param discussion Discussion 类型
     * @return 实体 R 类型
     */
    @Override
    public R addDiscussion(Discussion discussion) {

        R r = new R();

        int i = discussionMapper.addDiscussion(discussion);

        if(i != 0) {
            r.setCode(0);
        } else {
            r.setCode(1);
        }
        return r;
    }

    /**
     * 管理用通过 id 进行删除评论
     *
     * @param id int 类型表示评论的序号
     * @return 实体 R 类型
     */
    @Override
    public R deleteDiscussionById(Integer id) {
        R r = new R();

        int i = discussionMapper.deleteDiscussionById(id);

        if(i != 0) {
            r.setCode(0);
        } else {
            r.setCode(1);
        }
        return r;
    }
}
